(ns scripts.cljmon.process
  (:use util.mon
        util.utils)
  (:require clojure.string))


(defn jboss-process-filter [pid]
  ((set (get-process-args pid)) "org.jboss.Main"))

(def process-filters {"jboss" jboss-process-filter})

(defn- get-process-filter [name]
  (when-let [[_ k] (re-find #":(\w+)$" name)]
    (get process-filters k)))

(defn- get-original-process-name [name]
  (clojure.string/replace name #":(\w+)$" ""))

(defn- find-processes-with-filter [name process-filter]
  (doall (filter process-filter (find-pids-by-name name))))

(defn find-process [name]
  (if-let [process-filter (get-process-filter name)]
    (as-one (find-processes-with-filter (get-original-process-name name) process-filter) name)
    (find-pid-by-pid-or-name name)))

(defn find-all-processes [name]
  (if-let [process-filter (get-process-filter name)]
    (find-processes-with-filter (get-original-process-name name) process-filter)
    (find-pids-by-name name)))




